﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Drawing;
using System.Windows.Forms;
using Fireball.Windows.Forms;
using Fireball.Syntax;
using Fireball.CodeEditor.SyntaxFiles;
using DBConnectionLib;


namespace DBSourceToolsLib
{
    public abstract class DatabaseScript : Interfaces.IDisplayPanel
    {
        protected DatabaseEx m_ParentDatabase;
        protected DatabaseEx m_SourceDatabase;
        //protected string m_BaseDirectory;
        //protected string m_DatabaseName;
        private string m_ScriptText;
        private string m_ScriptFilePath;

        public DatabaseScript(DatabaseEx oParentDatabase, DatabaseEx oSourceDatabase)
        {
            this.m_ParentDatabase = oParentDatabase;
            this.m_SourceDatabase = oSourceDatabase;
            //this.m_DatabaseName = strDatabaseName;
            //this.m_BaseDirectory = strBaseDirectory;
        }

        #region getters and setters

        public string ScriptText
        {
            get {
                return m_ScriptText; 
            }
            set { m_ScriptText = value; }
        }
        
        public string ScriptFilePath
        {
            get { return m_ScriptFilePath; }
            set { m_ScriptFilePath = value; }
        }
        #endregion 

        public abstract void WriteDefaultScript();

        public void WriteScript()
        {
            string strScriptFileDir = this.m_ScriptFilePath.Substring(0, this.m_ScriptFilePath.LastIndexOf("\\"));
            if (!System.IO.Directory.Exists(strScriptFileDir))
                System.IO.Directory.CreateDirectory(strScriptFileDir);
            FileUtils.EnsureFileIsWriteable(this.m_ScriptFilePath);
            using (System.IO.StreamWriter oWriter = new System.IO.StreamWriter(this.m_ScriptFilePath))
            {
                oWriter.Write(m_ScriptText);
            }
        }

        public void LoadScriptFromDisk()
        {
            if (System.IO.File.Exists(this.ScriptFilePath))
            {
                using (System.IO.StreamReader oReader = new System.IO.StreamReader(this.ScriptFilePath))
                {
                    this.ScriptText = oReader.ReadToEnd();
                }

            }

        }

        public void LoadScriptFromDatabase()
        {
            if (!System.IO.Directory.Exists(this.m_ParentDatabase.BaseDirectory))
                System.IO.Directory.CreateDirectory(this.m_ParentDatabase.BaseDirectory);
            WriteDefaultScript();
            SetTextFromFile();

            this.ScriptText = this.ScriptText.Replace(this.m_SourceDatabase.DatabaseName, this.m_ParentDatabase.DatabaseName);
            WriteScript();

        }

        #region IDisplayPanel Members

        public System.Windows.Forms.Panel GetPanel()
        {
            Panel oPanel = new Panel();
            oPanel.Dock = DockStyle.Fill;

            CodeEditorControl codeEdit = new CodeEditorControl();
            codeEdit.Dock = DockStyle.Fill;

            CodeEditorSyntaxLoader.SetSyntax(codeEdit, SyntaxLanguage.SqlServer2K);
            codeEdit.Document.Text = this.ScriptText;
            oPanel.Controls.Add(codeEdit);

            return oPanel;
        }

        #endregion

        protected string GetStringFromColl(StringCollection myStrColl)
        {
            StringBuilder oStringBuilder = new StringBuilder();

            foreach (string strLine in myStrColl)
                oStringBuilder.AppendLine(strLine);
            return oStringBuilder.ToString();

        }

        protected void SetTextFromFile()
        {
            if (System.IO.File.Exists(this.ScriptFilePath))
            {
                using (System.IO.StreamReader oStreamReader = new System.IO.StreamReader(this.ScriptFilePath))
                {
                    this.ScriptText = oStreamReader.ReadToEnd();
                }

            }

        }
    }
}
